package com.qf.controller;

import com.qf.pojo.Goods;
import com.qf.service.GoodsService;
import com.qf.service.impl.GoodsServiceImpl;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/goods")
public class GoodsCotroller extends BaseServlet{
    public String index(HttpServletRequest request,HttpServletResponse response){
        GoodsService goodsService=new GoodsServiceImpl();
        List<Goods> list = goodsService.findGoodsAll();
        request.setAttribute("goodsList",list);
        return "forward:/index.jsp";
    }
    public  String detail(HttpServletRequest request,HttpServletResponse response){
        String id = request.getParameter("id");
        GoodsService goodsService=new GoodsServiceImpl();
        Goods goods= goodsService.findGoodById(Integer.parseInt(id));
        request.setAttribute("goods",goods);
        return "forward:/goodsDetail.jsp";
    }
    public String show(HttpServletRequest request,HttpServletResponse response){
        GoodsService goodsService=new GoodsServiceImpl();
        List<Goods> list = goodsService.findGoodsAll();
        request.setAttribute("goodsList",list);
        return "forward:/admin/showGoods.jsp";
    }
    public String addGoods(HttpServletRequest request,HttpServletResponse response)throws Exception{
        DiskFileItemFactory factory= new DiskFileItemFactory();
        ServletFileUpload parser = new ServletFileUpload(factory);
        parser.setHeaderEncoding("utf-8");
        if(!ServletFileUpload.isMultipartContent(request)){
            return "redirect:/admin/admin.jsp";
        }
        List<FileItem> fileItems= parser.parseRequest(request);
        String uploadPath = request.getServletContext().getRealPath("/files");
        System.out.println("========"+uploadPath);
        File fileDir = new File(uploadPath);
        if(!fileDir.exists()){
            fileDir.mkdirs();
        }
        Goods goods=new Goods();
        for (FileItem fileItem:fileItems){
            if(fileItem.isFormField()) {
                if (fileItem.getString() == null || "".equals(fileItem.getString())) {
                    continue;
                }
                if("typeid".equals(fileItem.getFieldName())){
                    goods.setTid(Integer.parseInt(fileItem.getString()));
                }
                if("name".equals(fileItem.getFieldName())){
                    goods.setName(new String(fileItem.getString().getBytes("ISO-8859-1"),"utf-8"));
                }
                if("pubdate".equals(fileItem.getFieldName())){
                    String dateStr = fileItem.getString();
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                    goods.setPubdate(sdf.parse(dateStr));
                }
                if("price".equals(fileItem.getFieldName())){
                    goods.setPrice(Integer.parseInt(fileItem.getString()));
                }
                if("star".equals(fileItem.getFieldName())){
                    goods.setStar(Integer.parseInt(fileItem.getString()));
                }if("intro".equals(fileItem.getFieldName())){
                    goods.setIntro(new String(fileItem.getString().getBytes("ISO-8859-1"),"utf-8"));
                }

            }else {
                String filePath = fileItem.getName();
                if(filePath==null||"".equals(filePath)){
                    continue;
                }
                String fileName = filePath.substring(filePath.lastIndexOf("\\") + 1);
                InputStream inputStream = fileItem.getInputStream();
                FileOutputStream out= new FileOutputStream(uploadPath +"\\" +fileName);
                System.out.println("========="+uploadPath);
                byte[] buffer=new byte[1024];
                int len=0;
                while ((len=inputStream.read(buffer))>0){
                    out.write(buffer,0,len);
                }
                out.close();
                inputStream.close();
                goods.setPicture("http://localhost:8080/files/"+fileName);

            }
        }
        GoodsService goodsService=new GoodsServiceImpl();
        goodsService.addGoods(goods);
        return "redirect:/admin/showGoods.jsp";
    }
}
